<?php
namespace Business\Controller;
use Think\Controller;
use Think\AjaxPage;
use Think\Page;
class UserController extends Controller {
    public function _initialize() {
        $this->assign('cur','user');
    }
    public function index(){
        $bid = session('bid');
        if($bid) {
            $mod = M('business');
            $info = $mod->where(array('id'=>$bid))->find();
            $this->assign('info',$info);
            $this->display();
        }else{
            header('Location:'.U('/Business/Index/index/'));
        }
    }

    public function store(){
        $bid = session('bid');
        if($bid) {
            $mod = M('business');
            if(IS_POST){
                $store_name = i('store_name')?i('store_name'):'';
                if(!$store_name){
                    exit(json_encode(array('code'=>100,'msg'=>'请输入商铺号或名称')));
                }
                session('store_name',$store_name);
                $result = $mod->where(array('id'=>$bid))->save(array('store_name'=>$store_name,'up_time'=>time()));
                if($result){
                    exit(json_encode(array('code'=>200,'msg'=>'修改成功')));
                }else{
                    exit(json_encode(array('code'=>100,'msg'=>'修改失败')));
                }
            }
            $act = i('act')?i('act'):'';
            $this->assign('act',$act);
            $this->display();
        }else{
            header('Location:'.U('/Business/Index/index/'));
        }
    }

    public function goods(){
        $bid = session('bid');
        if($bid) {
            $cate_mod = M('goods_cat');
            $cate_list = $cate_mod->where(array('cat_id'=>0))->order('sort_order desc,id asc')->select();
            $this->assign('cate_list',$cate_list);
            $this->display();
        }else{
            header('Location:'.U('/Business/Index/index/'));
        }
    }

    public function goods_list(){
        $bid = session('bid');
        if($bid) {
            $cat_id = i('cat_id')?i('cat_id'):'';
            $cate_mod = M('goods_cat');
            $cate_list = $cate_mod->where(array('cat_id'=>$cat_id))->order('sort_order desc,id asc')->select();
            $this->assign('cate_list',$cate_list);
            $this->assign('cat_id',$cat_id);
            $sid = i('sid')?i('sid'):$cate_list[0]['id'];
            $this->assign('sid',$sid);
            $this->display();
        }else{
            header('Location:'.U('/Business/Index/index/'));
        }
    }

    public function goods_shelves(){
        $bid = session('bid');
        if($bid) {
            $cat_id = i('cat_id')?i('cat_id'):'';
            $this->assign('cat_id',$cat_id);
            $this->display();
        }else{
            header('Location:'.U('/Business/Index/index/'));
        }
    }

    public function ajaxGoodsList(){
        $mod = M('goods_store_spec');
        $bid = session('bid');
        if($bid) {
            $sid = i('sid')?i('sid'):'';
            $where = array('b.second_cat_id' => $sid,'a.store_id'=>$bid,'a.status'=>1);
            $count = $mod
                ->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->where($where)
                ->count();
            $page_count = 10;
            $now_page = I('page') ? I('page') : 1;
            $Page = new AjaxPage($count, $page_count);
            $total_pages = ceil($count / $page_count);
            $is_page = 0;
            if ($now_page >= $total_pages) {
                $is_page = 1;
            }
            $goods_list = $mod
                ->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->join('left join ' . C('DB_PREFIX') . 'business as c on c.id=a.store_id')
                ->where($where)
                ->field('a.id as spec_id,a.is_sold,a.spec_price as shop_price,a.spec_mk_price,a.spec_name,b.keywords,a.up_time,b.id,b.goods_name,b.thumb,a.store_id,c.store_name')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
            foreach((array)$goods_list as $k=>$v){
                $goods_list[$k]['left_price'] = intval($v['shop_price']);
                $goods_list[$k]['right_price'] = substr($v['shop_price'], -3, 3);
            }
            exit(json_encode(array('is_page' =>$is_page, 'result' => $goods_list)));
        }else{
            header('Location:'.U('/Business/Index/index/'));
        }
    }

    public function ajaxGoodsShelves(){
        $mod = M('goods_store_spec');
        $bid = session('bid');
        if($bid) {
            $where = array('a.store_id'=>$bid,'a.status'=>0);
            $count = $mod
                ->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->where($where)
                ->count();
            $page_count = 10;
            $now_page = I('page') ? I('page') : 1;
            $Page = new AjaxPage($count, $page_count);
            $total_pages = ceil($count / $page_count);
            $is_page = 0;
            if ($now_page >= $total_pages) {
                $is_page = 1;
            }
            $goods_list = $mod
                ->alias('as a')
                ->join('left join ' . C('DB_PREFIX') . 'goods as b on a.goods_id=b.id')
                ->join('left join ' . C('DB_PREFIX') . 'business as c on c.id=a.store_id')
                ->where($where)
                ->field('a.id as spec_id,a.spec_price as shop_price,a.spec_name,b.keywords,a.up_time,b.id,b.goods_name,b.thumb,a.store_id,c.store_name')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
            foreach((array)$goods_list as $k=>$v){
                $goods_list[$k]['left_price'] = intval($v['shop_price']);
                $goods_list[$k]['right_price'] = substr($v['shop_price'], -3, 3);
            }
            exit(json_encode(array('is_page' =>$is_page, 'result' => $goods_list)));
        }else{
            header('Location:'.U('/Business/Index/index/'));
        }
    }

    //修改价格
    function check_goods(){
        $mod = M('goods_store_spec');
        $bid = session('bid');
        if($bid) {
            $spec_id = i('spec_id')?i('spec_id'):'';
            $spec_price = i('spec_price')?i('spec_price'):'';
            $spec_mk_price = i('spec_mk_price')?i('spec_mk_price'):'';
            if($spec_mk_price && $spec_mk_price>$spec_price){
                exit(json_encode(array('code'=>100,'msg'=>'特价金额不能大于正价金额')));
            }
            $result = $mod->where(array('id'=>$spec_id))->save(array('spec_price'=>$spec_mk_price?$spec_mk_price:$spec_price,'spec_mk_price'=>$spec_price,'up_time'=>time()));
            if($result){
                exit(json_encode(array('code'=>200,'msg'=>'修改成功')));
            }
        }
    }
	//提现
	function withdraw(){
		$bid = session('bid');
		if($bid) {
			$mod = M('business');
			$store = $mod->where(array('id'=>$bid))->field('store_price,withdraw_price')->find();
			$bw_mod = M('business_withdraw');
			if(i('act')=='ajax'){
				$data = i('post.');
				if($data['price']>$store['store_price']){
					exit(json_encode(array('code'=>100,'msg'=>'提现金额大于余额')));
				}
				$data['add_time'] = time();
				$data['store_id'] = $bid;
				$result = $bw_mod->add($data);
				if($result){
					$mod->where(array('id'=>$bid))->save(array('store_price'=>$store['store_price']-$data['price'],'withdraw_price'=>$store['withdraw_price']+$data['price']));
					M('business_log')->add(array(
						'order_sn'=>'--',
						'store_id'=>$bid,
						'type_id'=>2,
						'price'=>$data['price'],
						'add_time'=>time()
					));
					exit(json_encode(array('code'=>200,'msg'=>'提现成功')));
				}
			}
			$this->assign('store_price',$store['store_price']);
			$this->display();
		}
	}
	
	function history(){
		$bid = session('bid');
		if($bid) {
			$mod = M('business');
			$log_mod = M('business_log');
			$bw_mod = M('business_withdraw');
			$store_price = $mod->where(array('id'=>$bid))->getField('store_price');
			$start_time = strtotime(date('Y-m-d'));
			$end_time = strtotime(date('Y-m-d').' 23:59:59');
			$day_price = $log_mod->where(array('store_id'=>$bid,'type_id'=>1,'add_time'=>array('between',array($start_time, $end_time))))->sum('price');
			$this->assign('store_price',$store_price);
			$this->assign('day_price',$day_price?$day_price:'0.00');
			$log_list = $log_mod->where(array('store_id'=>$bid))->order('id desc')->select();
			foreach((array)$log_list as $k=>$v){
				if($v['wid']){
					$log_list[$k]['status'] = $bw_mod->where(array('id'=>$v['wid']))->getField('status');
				}
			}
			$this->assign('log_list',$log_list);
			$this->display();
		}
	}
    //设为售罄
    function sell_out(){
        $mod = M('goods_store_spec');
        $bid = session('bid');
        if($bid) {
            $spec_id = i('spec_id')?i('spec_id'):'';
            $num = i('num')?i('num'):'';
            if(!$spec_id){
                exit(json_encode(array('code'=>100,'msg'=>'请先选中商品')));
            }
            if($num==1){
                $data['is_sold'] = 1;
            }else if($num==2){
                $data['status'] = 0;
            }
            $result = $mod->where(array('id'=>array('in',rtrim($spec_id,',')),'store_id'=>$bid))->save($data);
            if($result){
                exit(json_encode(array('code'=>200,'msg'=>'修改成功')));
            }else{
                exit(json_encode(array('code'=>100,'msg'=>'修改失败')));
            }
        }
    }
    //设为上架
    function sell_shelves(){
        $mod = M('goods_store_spec');
        $bid = session('bid');
        if($bid) {
            $spec_id = i('spec_id')?i('spec_id'):'';
            if(!$spec_id){
                exit(json_encode(array('code'=>100,'msg'=>'请先选中商品')));
            }
            $data['status'] = 1;
            $data['up_time'] = time();
            $result = $mod->where(array('id'=>array('in',rtrim($spec_id,',')),'store_id'=>$bid))->save($data);
            if($result){
                exit(json_encode(array('code'=>200,'msg'=>'修改成功')));
            }else{
                exit(json_encode(array('code'=>100,'msg'=>'修改失败')));
            }
        }
    }
}